/**
 * 格式化饼图类
 * @des {
 *   仅格式化组件所需要的数据
 * }
 */
import { $each, $isEmptyArr, $isNullOrEmpty, isEmptyArr } from '../index';
import { handleConditionExtend } from '../conditionFormat/useConditionFormat.js';
import { processXAis } from './lineAndBarFormat.js';
/**
 * 格式化基础饼图 series
 * @param {object} datasets
 */
export function processBasicPieData(datasets) {
  const { dimensions, source, fieldNameChainMap, conditionData, conditionOptions } = datasets;
  const { yAxis, xAxis } = dimensions;
  const sourceData = [];
  // 判断是否是多维度
  let xAxisArr = [];
  if (xAxis) {
    xAxisArr = [xAxis];
    if (xAxis.includes('&')) {
      xAxisArr = xAxis.split('&');
    }
  }
  //不拖入度量,seriesData为空数组
  !$isNullOrEmpty(yAxis) && $each(source, (item) => {
    let XAXIS = '';
    if (xAxisArr.length) {
      xAxisArr.forEach((xAxisItem, index) => {
        if (item[xAxisItem]) {
          if (index === 0) {
            XAXIS = item[xAxisItem];
          } else {
            XAXIS += `-${item[xAxisItem]}`;
          }
        }
        // 根据原始字段名称收集数据
        if (fieldNameChainMap?.xAxis[xAxisItem]) {
          conditionData[fieldNameChainMap?.xAxis[xAxisItem]].push({ value: item[xAxisItem] });
        }
      });
    }
    sourceData.push({
      value: item[yAxis],
      values: XAXIS.includes('-') ? XAXIS.split('-'): [XAXIS],
      name: XAXIS,
    });
  });
  const seriesData = $isEmptyArr(sourceData) ? sourceData :[{
    name: yAxis,
    dataFormatType: 'yAxis',
    data: sourceData,
  }];
  // 根据原始字段名称收集数据
  if (yAxis && fieldNameChainMap?.yAxis[yAxis]) {
    conditionData[fieldNameChainMap.yAxis[yAxis]].push(...sourceData);
  }
  // 条件格式处理
  const options = {
    conditionData,
    isExistLegend: false, // 是否存在颜色维度
    seriesData,
    legendFieldName: null,
    fieldNameChainMap,
  };
  handleConditionExtend(options, conditionOptions);
  return {
    seriesData,
  };
}

/**
 * 格式化对比环形图 series
 * @param {object} datasets
 */
export function processPieDoubleData(datasets) {
  const { dimensions, source, fieldNameChainMap, conditionData, conditionOptions, type } = datasets;
  const { yAxis } = dimensions;
  const sourceData = [];
  // 判断是否是多维度
  let yAxisArr = [];
  if (yAxis) {
    yAxisArr = [yAxis];
    if (yAxis.includes('&')) {
      yAxisArr = yAxis.split('&');
    }
  }
  yAxisArr.forEach(yItem => {
    const yData = [];
    source.forEach(item => {
      item[yItem] && yData.push({
        value: item[yItem],
        name: type === 'STATIC' ? item.name :yItem,
      });
    });
    sourceData.push(...yData);
    // 根据原始字段名称收集数据
    if (fieldNameChainMap['yAxis'] && fieldNameChainMap?.yAxis[yItem]) {
      conditionData[fieldNameChainMap.yAxis[yItem]].push(...yData);
    }
  });
  const seriesData = $isEmptyArr(sourceData) ? sourceData :[{
    name: yAxis,
    dataFormatType: 'yAxis',
    data: sourceData,
  }];
  // 条件格式处理
  const options = {
    conditionData,
    isExistLegend: false, // 是否存在颜色维度
    seriesData,
    legendFieldName: null,
    chartTag: 'pieDouble',
    fieldNameChainMap,
  };
  handleConditionExtend(options, conditionOptions);
  return {
    seriesData,
  };
}

/**
 * 格式化仪表盘 series
 * @param {object} datasets
 * @param {object} option
 */
export function pieDashBoardFormat(datasets, others = {}) {
  const { dimensions, source, fieldNameChainMap, conditionData, conditionOptions, sourceMap = {} } = datasets;
  const { yAxis, xAxis, yAxisLine, yAxisBar, yAxisCondition } = dimensions;
  const { dataFormatType, chartType } = others;
  let yAxisConditionArr = [];//用来单独存储条件项名称
  // 条件项存在多度量
  if (yAxisCondition && yAxisCondition.includes('&')) {
    yAxisConditionArr = yAxisCondition.split('&');
  } else {
    yAxisConditionArr = [yAxisCondition];
  }
  // IOT组件返回值用name而不是别名对应数据会存在找不到
  let yAxisIotMap = [];
  for (let item in sourceMap) {
    yAxisIotMap[item] = sourceMap[item].name;
  }
  const [yAxisIot] = Object.keys(sourceMap);
  // 处理维度值
  processXAis(datasets);
  const sourceData = [];
  $each(source, (item) => {
    sourceData.push({
      value: item[yAxisIot] || item[yAxis],
      name: yAxis,
      dataFormatType
    });
  });
  // 如果维度存在，那么只取第一个，如饼环图里面的仪表盘
  if ((sourceData.length && xAxis) || chartType ) {
    if (sourceData.length > 1) {
      // 只取第一个
      sourceData.splice(1);
    }
  }
  const item  = {
    name: yAxis,
    data: sourceData,
    dataFormatType,
  };
  //iot组件需要存时间字段
  if(chartType === 'iot' && !isEmptyArr(source)) {
    item.date = source[0]?.ts;
  };
  const seriesData = [item];
  // 根据原始字段名称收集数据
  if (yAxis && fieldNameChainMap?.yAxis[yAxis]) {
    conditionData[fieldNameChainMap.yAxis[yAxis]].push(...sourceData);
  }
  // 条件项存在
  if(!$isEmptyArr(yAxisConditionArr)) {
    yAxisConditionArr.forEach((yItem) => {
      const yConditionData = [];
      source.forEach((item) => {
          yConditionData.push({
            value: item[yItem],
          });
      });
      // 根据原始字段名称收集数据
      if(fieldNameChainMap['yAxisCondition'] && fieldNameChainMap['yAxisCondition'][yItem]) {
        conditionData[fieldNameChainMap['yAxisCondition'][yItem]].push(...yConditionData);
      }
    });
  }
  // 条件格式处理
  const options = {
    conditionData,
    isExistLegend: false, // 是否存在颜色维度
    seriesData,
    legendFieldName: null,
    fieldNameChainMap,
  };
  handleConditionExtend(options, conditionOptions);
  return {
    seriesData
  };
}
/**
 * 格式化累积值环形图series
 * @param {object} datasets
 * @param {object} option
 */
export function pieAccumulateFormat(datasets) {
  const { dimensions, source } = datasets;
  const { yAxis } = dimensions;
  let dataSum = 0;
  const sourceData = [];
  $each(source, (item) => {
    if(!$isNullOrEmpty(item[yAxis])) {
      dataSum += item[yAxis]
    }
  });
  !$isNullOrEmpty(yAxis) && sourceData.push({value:dataSum});
  return {
    seriesData: [{ data: sourceData, name: yAxis }]
  }
}
/**
 * 格式化多系列指针仪表盘series
 * @param {object} datasets
 * @param {object} option
 */
export function pieGaugeMultillFormat(datasets) {
  const { dimensions, source , type} = datasets;
  const { yAxis  } = dimensions;
  let yAxisArr = [];
  let yData = [];
  if (yAxis) {
    yAxisArr = [yAxis];
    if (yAxis.includes('&')) {
      yAxisArr = yAxis.split('&');
    }
  }
  yAxisArr.forEach(yAxisItem=>{
    source.forEach(item=>{
      const name =  type ==='STATIC'? item.name : yAxisItem
      yData.push({data:[{
        name: name,
        value: item[yAxisItem]
      }],name});
    })
  })
  return {
    seriesData: yData
  }
}
